package utils

import java.net.URLEncoder

import play.api.mvc.RequestHeader

object DownloadUtils {

  def contentDispositionByName(fileName: String)(
    implicit
    request: RequestHeader): String = {
    val isFirefox = request.headers
      .get("User-Agent")
      .map { s =>
        s.toLowerCase().indexOf("firefox") > -1
      }
      .getOrElse(false)
    if (isFirefox) {
      s"attachment;filename*=UTF-8''${URLEncoder.encode(fileName, "utf-8")}"
    } else {
      s"attachment;filename=${URLEncoder.encode(fileName, "utf-8")}"
    }
  }

}
